/***********************自定义查询控件类开始*******************/
	// 定义一个控件类,即function---自定义查询控件
	function SearchControl(suggest_id){
	  // 默认停靠位置和偏移量
	  this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
	  this.defaultOffset = new BMap.Size(10, 10);
	  this.suggest_id=suggest_id;
	}
	
	// 通过JavaScript的prototype属性继承于BMap.Control
	SearchControl.prototype = new BMap.Control();
	
	// 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
	// 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
	SearchControl.prototype.initialize = function(map){
		  // 创建一个DOM元素
		  var div = document.createElement("div");
		  //创建一个输入框
		 // var label=document.createElement('label');
		 // label.innerHTML = "搜索：";
		  
		  var input=document.createElement('input');
		  input.setAttribute('type', 'text');
		  input.setAttribute("id",  this.suggest_id);
		  // 添加文字说明
		 // div.appendChild(label);
		  div.appendChild(input);
		  var btn=document.createElement("a");
		  btn.appendChild(document.createTextNode("搜索"));
		  // 绑定事件,点击一次放大两级
		  btn.onclick=function(){
			  var value=input.value;
			  if(value=="")return;
			  setPlace(value);
		  };
		  div.appendChild(btn);
		  // 设置样式
		  div.style.cursor = "pointer";
		  div.style.border = "1px solid gray";
		  div.style.backgroundColor = "white";
	
		  // 添加DOM元素到地图中
		  map.getContainer().appendChild(div);
		  // 将DOM元素返回
		  return div;
	};
	
/**************************自定义查询控件结束**************************************/
	
/*****
 * 百度地图插件v0.0.1
 * 
 */

;(function($) {
	// These methods can be called by adding them as the first argument in the uploadify plugin call
	var methods = {

		init : function(options) {
			return this.each(function() {
				var $this = $(this);
				// Setup the default options
				var settings = $.extend({
					// Required Settings
					id       : $this.attr('id'),
					map_id   :'bd_map'+$this.attr('id'),
					suggest_id:'suggest_id'+$this.attr('id'),
					default_x:'116.404',
					default_y:'39.915',
					map_location_x:$this.attr('id')+"_map_location_x",
					map_location_y:$this.attr('id')+"_map_location_y",
					default_zoom_size:18,
					width:600,
					height:350,
					help:'请红色图标进行选择坐标。'
					}
				,options);
				// Add the object to the elements data object
				$this.data('settings', settings);
				//获取已经存在的经纬度，然后直接复制查询
				var x_y_value=$this.val();
				var _x_map_value=settings.default_x;
				var _y_map_value=settings.default_y;
				if(x_y_value!=""){
					var array_values_map=x_y_value.split(',');
					_x_map_value=array_values_map[0];
					_y_map_value=array_values_map[1];
				}
				//创建一些列dom对象
				var _show_xy_input=$('<div/>').attr("id",settings.id+"_show").append('<label>经度：</label><input id="'+settings.map_location_x+'">').append('<label>纬度：</label><input id="'+settings.map_location_y+'">');
				$this.after(_show_xy_input);
				var _help_p=$('<p/>').addClass('help-block').html(settings.help);
				_show_xy_input.after(_help_p);
				//map_dom
				var _map_div=$('<div/>').attr('id',settings.map_id).css('width',settings.width).css('height',settings.height);
				_help_p.after(_map_div);
				///创建dom结束======================
				var map = new BMap.Map(settings.map_id);            // 创建Map实例
				var cpoint = new BMap.Point(_x_map_value,_y_map_value); // 创建点坐标
				map.centerAndZoom(cpoint,settings.default_zoom_size);                 
				map.enableScrollWheelZoom();                 //启用滚轮放大缩小
				$this.data('map', map);
				//设置当前地理位置
				//overMark();
				$this.data('cpoint', cpoint);
				var marker=$('#' + settings.id).maper('overMark');
				$this.data('marker', marker);
				$this.val(cpoint.lng+","+cpoint.lat);
				$('#'+settings.map_location_x).val(cpoint.lng);
				$('#'+settings.map_location_y).val(cpoint.lat);
				//定位服务
				if(x_y_value==""){
					var myCity = new BMap.LocalCity();
					myCity.get(function(result){
						var cityName = result.name;
						map.setCenter(cityName);
						console.log("当前定位城市:"+cityName);
					});
				}
				//添加平移
				var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件
				map.addControl(top_left_navigation); 
				//创建查询控件
				var searchControl = new SearchControl(settings.suggest_id);
				// 添加到地图当中
				map.addControl(searchControl);
				//智能提示对象
				//添加输入自动提示
				var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
					{"input" : settings.suggest_id
					,"location" : map
				});
				ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
				    var str = "";
					var _value = e.fromitem.value;
					var value = "";
					if (e.fromitem.index > -1) {
						value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
					}    
					str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
					
					value = "";
					if (e.toitem.index > -1) {
						_value = e.toitem.value;
						value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
					}    
					str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
				});
				ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
					var _value = e.item.value;
					var myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
					//setPlace(myValue);
					$('#' + settings.id).maper('setPlace',myValue);
				});
				
			});
		},
		//创建标注
		overMark:function(){
				var $this=$(this);
				var settings=$this.data('settings');
				var map=$this.data('map');
				var cpoint=$this.data('cpoint');
			    var marker = new BMap.Marker(cpoint);  // 创建标注
				map.addOverlay(marker);               // 将标注添加到地图中
				//marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
				marker.enableDragging();           // 不可拖拽
				//监听事件
				marker.addEventListener("dragend",function(e){
					$('#'+settings.map_location_x).val(e.point.lng);
					$('#'+settings.map_location_y).val(e.point.lat);
					$this.val(e.point.lng+","+e.point.lat);
					map.centerAndZoom(e.point,settings.default_zoom_size);
				});
				return marker;
		},
		setPlace:function(myValue){
			    $this=$(this);
			    var settings=$this.data('settings');
			    var map=$this.data('map');
			    var marker=$this.data('marker');
				var local = new BMap.LocalSearch(map, { //智能搜索
				  onSearchComplete: replaceMarker
				});
				function replaceMarker(){
					 var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
					 map.centerAndZoom(pp, settings.default_zoom_size);
					 $('#'+settings.map_location_x).val(pp.lng);
		     		 $('#'+settings.map_location_y).val(pp.lat);
		     		 $this.val(pp.lng+","+pp.lat);
					 marker.setPosition(pp);
			         map.addOverlay(marker);
				}
				local.search(myValue);
		}
	};
	
    	$.fn.maper = function(method) {

    		if (methods[method]) {
    			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    		} else if (typeof method === 'object' || !method) {
    			return methods.init.apply(this, arguments);
    		} else {
    			$.error('The method ' + method + ' does not exist in $.maper');
    		}

    	};
    	
})($);

/****
	var map;
    var cpoint;//当前坐标点
    var marker;//当前覆盖物
    var cityName;//当前城市
	var DEFAULT_ZOOM_SIZE=18;
	var myValue;//当前查询值
	
	function map_init() {
		map = new BMap.Map("b_map");            // 创建Map实例
		cpoint = new BMap.Point(116.404, 39.915); // 创建点坐标
		map.centerAndZoom(cpoint,DEFAULT_ZOOM_SIZE);                 
		map.enableScrollWheelZoom();                 //启用滚轮放大缩小
		//设置当前地理位置
		overMark();
		//定位服务
		var myCity = new BMap.LocalCity();
		myCity.get(gps_location);
		//添加平移
		var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件
		map.addControl(top_left_navigation); 
		//创建查询控件
		var searchControl = new SearchControl();
		// 添加到地图当中
		map.addControl(searchControl);
	}
	
	function gps_location(result){
		cityName = result.name;
		map.setCenter(cityName);
		console.log("当前定位城市:"+cityName);
	}
	
	function overMark(){
	    marker = new BMap.Marker(cpoint);  // 创建标注
		map.addOverlay(marker);               // 将标注添加到地图中
		//marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
		marker.enableDragging();           // 不可拖拽
		//监听事件
		marker.addEventListener("dragend",function(e){
			$('#map_location_x').val(e.point.lng);
			$('#map_location_y').val(e.point.lat);
			map.centerAndZoom(e.point,DEFAULT_ZOOM_SIZE);
		});
	}
	***/
	
